package com.linkoog.devtools.json.toolwindow;

import com.intellij.icons.AllIcons;
import com.intellij.ide.startup.StartupManagerEx;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowEx;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.impl.ContentImpl;
import com.intellij.util.ModalityUiUtil;
import com.linkoog.devtools.json.toolwindow.dialog.JsonAddDialog;
import com.linkoog.devtools.json.toolwindow.ui.JsonContentPanel;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;

public class JsonToolWindowManager implements Disposable {

    String TOOL_WINDOW_ID = "com.linkoog.devtools.JsonView";
    String TOOL_WINDOW_NAME = "Json View";
    private final Project project;

    public JsonToolWindowManager(@NotNull Project project) {
        this.project = project;
    }


    @Override
    public void dispose() {
    }

    public @NotNull ToolWindowEx getOrCreateToolWindow() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ToolWindowEx toolWindowEx = (ToolWindowEx)toolWindowManager.getToolWindow(TOOL_WINDOW_ID);
        if (toolWindowEx != null) {
            return toolWindowEx;
        }

        toolWindowEx = (ToolWindowEx)toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, builder -> {
            builder.stripeTitle = () -> TOOL_WINDOW_NAME;
            builder.icon = IconLoader.getIcon("/icons/json/json.png", JsonToolWindowManager.class);
            builder.canCloseContent = true;
            builder.hideOnEmptyContent = true;
            builder.anchor = ToolWindowAnchor.RIGHT;
            return Unit.INSTANCE;
        });
        toolWindowEx.setAutoHide(true);

        return toolWindowEx;
    }

    public void show(String json){
        invokeLaterIfNeeded(() -> {
            ToolWindowEx toolWindowEx = getOrCreateToolWindow();
            addContent(toolWindowEx, json);
            toolWindowEx.show();
        });
    }

    private void addContent(ToolWindowEx toolWindowEx, String json){
        final ContentManager contentManager = toolWindowEx.getContentManager();

        JsonContentPanel contentPanel = new JsonContentPanel(project, json);


        final int count = contentManager.getContentCount();
        ContentImpl content = new ContentImpl(contentPanel.getMyMainPanel(), "tab" + (count+1), false);

        contentManager.addContent(content);
        contentManager.setSelectedContent(content);
        if (count == 0){
            AnAction addDailogAction = new AnAction("add", "add", AllIcons.General.Add){
                @Override
                public void actionPerformed(@NotNull AnActionEvent e) {
                    Project project = e.getProject();
                    if (project == null) return;

                    JsonAddDialog jsonAddDialog = new JsonAddDialog(project);
                    jsonAddDialog.show();
                }
            };
            toolWindowEx.setTabActions(addDailogAction);
        }
    }


    public void addContent(String json) {
        invokeLaterIfNeeded(() -> {
            addContent(getOrCreateToolWindow(), json);
        });
    }

    public void removeContent(Content content) {
        invokeLaterIfNeeded(() -> {
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID);
            ContentManager contentManager = toolWindow == null ? null : toolWindow.getContentManager();
            if (contentManager != null && !contentManager.isDisposed()) {
                contentManager.removeContent(content, true);
            }
        });
    }

    private void invokeLaterIfNeeded(@NotNull Runnable runnable) {
        if (project.isDefault()) {
            return;
        }

        StartupManagerEx.getInstanceEx(project).runAfterOpened(() -> {
            ModalityUiUtil.invokeLaterIfNeeded(ModalityState.defaultModalityState(), project.getDisposed(), runnable);
        });
    }






}
